#include "widget.h"
#include "ui_widget.h"
#include<QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent()      //主绘制窗口
{

    if(pFrame->nWidth != 0 && pFrame->nHeight!= 0)
    {
        QImage img((unsigned char*)pFrame->pVideoData + pFrame->nWaveContentSize - 4, pFrame->nWidth, pFrame->nHeight, QImage::Format_RGB888);
//         ui->labelScreen->imageWidth=img.width();
//         ui->labelScreen->imageHeight=img.height();
//         ui->labelScreen->imageFrequency=pFrame->nFps;

         ui->labelScreen->offset_x=pWave->offset_x;
         ui->labelScreen->offset_y=pWave->offset_y;
         ui->labelScreen->offset_A=pWave->offset_A;
         ui->labelScreen->offset_E=pWave->offset_E;
         ui->labelScreen->nPosX=pWave->nPosX;
         ui->labelScreen->nPosY=pWave->nPosY;
         ui->labelScreen->imageWidth=pFrame->nWidth;
         ui->labelScreen->imageHeight=pFrame->nHeight;
         ui->labelScreen->imageFrequency=pFrame->nFps;
         ui->labelScreen->AVelocity=pWave->AVelocity;
         ui->labelScreen->EVelocity=pWave->EVelocity;
         ui->labelScreen->AForecastValue=pWave->AForecastValue;
         ui->labelScreen->EForecastValue=pWave->EForecastValue;

         if(ui->labelScreen->showBoMen)
         {

                 QPainter drawImagePainter(&img);
//                 drawImagePainter.setPen(QPen(Qt::red,1,Qt::DashLine));//设置画笔

                 qDebug()<<pFrame->nWaveCount;       //波门数量
                 qDebug()<<pFrame->nFps;

//                 QRect bomen( pWave->bomen_x,pWave->bomen_y,pWave->bomen_wide,pWave->bomen_height);
                 qDebug()<<pWave->bomen_x<<pWave->bomen_y<<pWave->bomen_wide<<pWave->bomen_height;
//                 drawImagePainter.drawRect(bomen);
                float x,y,w,h;
                x=pWave->bomen_x*1.0;
                y=pWave->bomen_y*1.0;
                w=pWave->bomen_wide*1.0;
                h=pWave->bomen_height*1.0;
                QPen pen;
                pen.setColor(Qt::blue);
                pen.setWidth(5);
                pen.setStyle(Qt::DashDotDotLine);
                drawImagePainter.setPen(pen);


                 drawImagePainter.drawLine(x,y+h/2,x+w,y+h/2);
                 drawImagePainter.drawLine(x+w/2,y,x+w/2,y+h); //设置对应十字虚线
                 QPen pen1;
                 pen1.setColor(Qt::blue);
                 pen1.setWidth(5);
                 pen1.setStyle(Qt::SolidLine);
                 drawImagePainter.drawLine(x,y,x+w/10,y);  //设置对应直角
                 drawImagePainter.drawLine(x,y,x,y+h/10);

                 drawImagePainter.drawLine(x,y+h,x+w/10,y+h);
                 drawImagePainter.drawLine(x,y+h,x,y+h*9/10);

                 drawImagePainter.drawLine(x+w,y,x+w*9/10,y);
                 drawImagePainter.drawLine(x+w,y,x+w,y+h/10);

                 drawImagePainter.drawLine(x+w,y+h,x+w*9/10,y+h);
                 drawImagePainter.drawLine(x+w,y+h,x+w,y+h*9/10);
                 drawImagePainter.end();
         }
        img=img.mirrored();
        ui->labelScreen->setPixmap(QPixmap::fromImage(img));

    }

}



void Widget::on_listWidget_currentRowChanged(int currentRow)  //列表文件
{
    if(currentRow!=-1)
    {
      if(timeflag)
        {
         timer->stop();
         ui->labelScreen->clear();
         ui->labelScreen->setStyleSheet("selection-color: rgb(229, 9, 9);"
                 "background-color: rgb(0, 0, 0);");
         ui->pBtnPlay->setStyleSheet("border-image: url(:/images/play.png);");
         ui->lineFrameEdit->setText(QString::number(0));
         ui->FreqlineEdit->setText(QString::number(50));
         ui->lineFrameEdit->setEnabled(false);
         ui->FreqlineEdit->setEnabled(false);
         ui->hztSlidProgress->setEnabled(false);
         ui->pBtnBack->setEnabled(false);
         ui->pBtnLast->setEnabled(false);
         ui->pBtnPrew->setEnabled(false);
         ui->pBtnUnder->setEnabled(false);
         ui->CurrentFrame->setText(QString::number(0));   //初始化当前帧为0
         ui->TotalFrameCount->setText(QString::number(0)); //初始化总帧数为0

         ui->hztSlidProgress->setValue(0);

        }
      qDebug()<<currentRow<<":::::";
      if(pFile)
      {
         CloseFile(pFile);      //关闭视频文件
      }
      qDebug()<<ui->listWidget->currentRow();
      qDebug()<<lpFiles[currentRow].szPathName;

      qDebug()<<lpFiles[currentRow].stStartTime.nYear<<lpFiles[currentRow].stStartTime.nMouth<<lpFiles[currentRow].stStartTime.nDay<<lpFiles[currentRow].stStartTime.nHour<<lpFiles[currentRow].stStartTime.nMinute<<lpFiles[currentRow].stStartTime.nSecond<<lpFiles[currentRow].stStopTime.nHour<<lpFiles[currentRow].stStopTime.nMinute<<lpFiles[currentRow].stStopTime.nSecond;
      qDebug()<<lpFiles[currentRow].nFps<<lpFiles[currentRow].nFrameType;

      ui->pBtnPlay->setEnabled(false);
      ui->pBtnUnder->setEnabled(false);
      ui->pBtnPrew->setEnabled(false);
      ui->pBtnLast->setEnabled(false);
      ui->pBtnBack->setEnabled(false);


    ui->listWidget->setCurrentRow(currentRow);
    fi=new QFileInfo(lpFiles[currentRow].szPathName);
    if(fi->isFile())
    {
         pFile = OpenAviFile(pSql, lpFiles[currentRow].szPathName);   //打开视频文件

        if(!pFile)
        {

             qDebug()<<"打开视频文件失败!";
        }
        else
        {
            ui->pBtnPlay->setEnabled(true);
            ui->pBtnUnder->setEnabled(true);
            ui->pBtnPrew->setEnabled(true);
            ui->pBtnLast->setEnabled(true);
            ui->pBtnBack->setEnabled(true);
            ui->hztSlidProgress->setEnabled(false);
            ui->lineFrameEdit->setEnabled(false);
            ui->pBtnFrameOk->setEnabled(false);
        }
        nFrameCount = GetTotalFrameCount(pFile);      //获取视频文件总帧数
        if(nFrameCount<0)
        {
            QMessageBox::information(this,tr("提示信息"),"打开视频文件失败!",QMessageBox::Ok);
            ui->TotalFrameCount->setText(QString::number(-1));

        }
        else
        {
        qDebug()<<"frame count:"<<nFrameCount<<endl;

        CurrentFrame=0;                               //设置当前帧第一帧

        ui->CurrentFrame->setText(QString::number(CurrentFrame));   //显示当前帧
        ui->TotalFrameCount->setText(QString::number(nFrameCount)); //显示总帧数

        ui->hztSlidProgress->setRange(0,nFrameCount-1);   //视频帧从0帧开始
        initime=1000/(ui->FreqlineEdit->text().toInt());
        mSectimer=initime;
        mlasttimer=initime;
        mbacktimer=initime;
        }


    }
    else
    {
        QMessageBox::information(this,tr("提示信息"),"视频文件不存在！",QMessageBox::Ok);
        CurrentFrame=0;                               //设置当前帧第一帧

        ui->CurrentFrame->setText(QString::number(CurrentFrame));   //显示当前帧
        ui->TotalFrameCount->setText(QString::number(0)); //显示总帧数

        pFile=NULL;
        delete fi;
    }

    }

}



void  Widget::on_pBtnFile1_clicked()    //打开本地文件
{
//    if(pFile)
//    {
//    CloseFile(pFile);       //关闭视频文件
//    }
    if(pSql)
    {
    CloseSqlite(pSql);      //关闭数据库
    }

   if(timeflag)
   {
       timer->stop();

       ui->labelScreen->clear();
       ui->labelScreen->setStyleSheet("selection-color: rgb(229, 9, 9);"
               "background-color: rgb(0, 0, 0);");
       ui->pBtnPlay->setStyleSheet("border-image: url(:/images/play.png);");
       ui->lineFrameEdit->setText(QString::number(0));
       ui->FreqlineEdit->setText(QString::number(50));
       ui->lineFrameEdit->setEnabled(false);
       ui->FreqlineEdit->setEnabled(false);
       ui->hztSlidProgress->setEnabled(false);
       ui->pBtnBack->setEnabled(false);
       ui->pBtnLast->setEnabled(false);
       ui->pBtnPrew->setEnabled(false);
       ui->pBtnUnder->setEnabled(false);
       ui->listWidget->setEnabled(false);
       ui->CurrentFrame->setText(QString::number(0));   //初始化当前帧为0
       ui->TotalFrameCount->setText(QString::number(0)); //初始化总帧数为0

       ui->hztSlidProgress->setValue(0);
   }

    ui->listWidget->clear();
    filedir = QFileDialog::getOpenFileName(this,tr("打开数据库文件"),"/",tr("*.db"));
    std::string str = filedir.toStdString();
    const char * dirlist=str.c_str();//数据库文件路径

    int pnCount=0;   //返回数目
    int i,nSize=sizeof(VIDEO_FILE_INFO);//缓冲大小

    pSql=OpenSqlite(dirlist);   //打开数据库文件句柄
    if(!pSql)
    {
        QMessageBox::critical(this,tr("警告"),"打开数据库失败!",QMessageBox::Ok);
        qDebug()<<"打开数据库失败！";
    }

    GetVideoFiles(pSql,NULL,-1,&pnCount);//获取视频文件列表
    if(pnCount<=0)
    {
        QMessageBox::critical(this,tr("警告"),"获取视频文件列表失败!",QMessageBox::Ok);
        qDebug()<<"获取视频文件列表失败！";
    }


    else
    {
        if(lpFiles)
        {
        delete []lpFiles;
        }

//        lpFiles=new struct tagVIDEO_FILE_INFO[pnCount];
        lpFiles=new VIDEO_FILE_INFO[pnCount];

     GetVideoFiles(pSql,lpFiles,nSize*pnCount,&pnCount);


    QStringList lpFilesList;
    QString    list;
    for(i=0;i<pnCount;i++)
    {
        list=lpFiles[i].szPathName;
        list=list.section('/',-1);
        lpFilesList<<list;
        qDebug()<<list;

    }
        ui->listWidget->setEnabled(true);
        ui->listWidget->addItems(lpFilesList);
    qDebug()<<lpFilesList.count();



    ui->listWidget->setCurrentRow(-1);     //设置当前行为-1
    }



}

void Widget::on_pBtnPlay_clicked()   //播放暂停视频
{


    timeflag=true;
    ui->hztSlidProgress->setEnabled(true);


    QString stylesheet1;
    stylesheet1=ui->pBtnPlay->styleSheet();

        qDebug()<<stylesheet1;


    if(stylesheet1=="border-image: url(:/images/play.png);")
    {
        playflag=true;
        ui->pBtnPlay->setStyleSheet("border-image: url(:/images/pause.png);");
        stylesheet1="border-image: url(:/images/pause.png);";
        qDebug()<<"播放";
        ui->lineFrameEdit->setEnabled(false);
        ui->pBtnFrameOk->setEnabled(false);
        ui->FreqlineEdit->setEnabled(false);
        ui->pBtnBack->setEnabled(true);
        ui->pBtnLast->setEnabled(true);
        ui->pBtnPrew->setEnabled(true);
        ui->pBtnUnder->setEnabled(true);
        ui->FreqlineEdit->setText(QString::number(FrequeLine));
         pFrame = (VideoFrame*)pBuf;



         pWave =(StSensorSingleObjData *)pFrame->pWaveData;



         timer=new QTimer(this);
         connect(timer,SIGNAL(timeout()),this,SLOT(changePicture()));
         timer->start(mSectimer);


    }
    else
    {
        playflag=false;
        ui->pBtnPlay->setStyleSheet("border-image: url(:/images/play.png);");
        stylesheet1="border-image: url(:/images/play.png);";
        qDebug()<<"暂停";
        ui->lineFrameEdit->setEnabled(true);
        ui->pBtnFrameOk->setEnabled(true);
        ui->FreqlineEdit->setEnabled(true);
        ui->pBtnBack->setEnabled(false);
        ui->pBtnLast->setEnabled(false);
        ui->pBtnPrew->setEnabled(false);
        ui->pBtnUnder->setEnabled(false);

        timer->stop();
    }
	

}

void Widget::changePicture()    //改变读取帧数
{

      if(lastbackflag)
      {
        if(CurrentFrame>nFrameCount-1|CurrentFrame<0)
        {


               CurrentFrame=0;
               ui->pBtnPlay->setStyleSheet("border-image: url(:/images/play.png);");
               ui->pBtnBack->setEnabled(false);
               ui->pBtnLast->setEnabled(false);
               ui->pBtnPrew->setEnabled(false);
               ui->pBtnUnder->setEnabled(false);
               lastbackflag=true;

               mSectimer=initime;
               timer->stop();


        }
        else
        {

            nRet = GetFrame(pFile, pFrame, 10*1024*1024,CurrentFrame); //获取视频文件当前帧


             if(nRet!=0|(CurrentFrame<0)|(CurrentFrame>=nFrameCount))
             {
                qDebug()<<"获取第"<<CurrentFrame<<"帧失败！";
                CurrentFrame++;


             }
             else
             {
                qDebug()<<"获取第"<<CurrentFrame<<"帧成功！"<<"hao";

                paintEvent();
             }
             ui->CurrentFrame->setText(QString::number(CurrentFrame));
             ui->hztSlidProgress->setValue(CurrentFrame);
             ++CurrentFrame;
        }
      }

      else
      {
                  if(CurrentFrame<0|CurrentFrame>nFrameCount-1)
                  {
                    CurrentFrame=0;
                    ui->pBtnPlay->setStyleSheet("border-image: url(:/images/play.png);");
                    mSectimer=initime;
                    ui->pBtnBack->setEnabled(false);
                    ui->pBtnLast->setEnabled(false);
                    ui->pBtnPrew->setEnabled(false);
                    ui->pBtnUnder->setEnabled(false);
                    timer->stop();
                    lastbackflag=true;

                  }
                  else
                  {

                       nRet = GetFrame(pFile, pFrame, 10*1024*1024,CurrentFrame); //获取视频文件当前帧
                         qDebug()<<nRet;
                       if(nRet!=0)
                      {
                         qDebug()<<"获取第"<<CurrentFrame<<"帧失败！";
                         CurrentFrame++;

                      }
                      else
                      {
                        qDebug()<<"获取第"<<CurrentFrame<<"帧成功！"<<"houtui";
                        paintEvent();
                      }
                       ui->CurrentFrame->setText(QString::number(CurrentFrame));
                       ui->hztSlidProgress->setValue(CurrentFrame);
                       --CurrentFrame;

                  }
      }


}



void Widget::on_pBtnClose_clicked() //关闭视频回放系统
{
   CloseFile(pFile);
   CloseSqlite(pSql);
   this->close();
}



void Widget::on_pBtnUnder_clicked() //慢进
{
    if(playflag)
    {
        lastbackflag=true;
        mbacktimer=initime;
        mSectimer=mlasttimer*2;
        mlasttimer=mSectimer;
        timer->start(mSectimer);
        FrequeLine=FrequeLine/2;
        if(!FrequeLine)
        {
            QMessageBox::critical(this,tr("警告"),"已经最慢，不能后退！",QMessageBox::Close);
            FrequeLine=1;
        }
        ui->FreqlineEdit->setText(QString::number(FrequeLine));
    }

}

void Widget::on_pBtnLast_clicked() //快进
{

     if(playflag)
     {
        lastbackflag=true;
        mbacktimer=initime;
        mSectimer=mlasttimer/2;
        mlasttimer=mSectimer;
        timer->start(mSectimer);
        FrequeLine=FrequeLine*2;
        if(FrequeLine>1000)
        {
            QMessageBox::critical(this,tr("警告"),"已经最快，不能快进！",QMessageBox::Close);
            FrequeLine=FrequeLine/2;
        }
        ui->FreqlineEdit->setText(QString::number(FrequeLine));

     }

}

void Widget::on_pBtnPrew_clicked() //慢退
{
     if(playflag)
     {
         lastbackflag=false;
         mlasttimer=initime;
         mSectimer=mbacktimer*2;
         mbacktimer=mSectimer;
         timer->start(mSectimer);
         FrequeLine=FrequeLine/2;
         if(!FrequeLine)
         {
             QMessageBox::critical(this,tr("警告"),"已经最慢，不能后退！",QMessageBox::Close);
             FrequeLine=1;
         }
         ui->FreqlineEdit->setText(QString::number(FrequeLine));

     }
}

void Widget::on_pBtnBack_clicked() //快退
{

  if(playflag)
  {

           lastbackflag=false;
           mlasttimer=initime;
           mSectimer=mbacktimer/2;
           mbacktimer=mSectimer;
           timer->start(mSectimer);
           FrequeLine=FrequeLine*2;
           if(FrequeLine>1000)
           {
           QMessageBox::critical(this,tr("警告"),"已经最快，不能快进！",QMessageBox::Close);
           FrequeLine=FrequeLine/2;
           }
           ui->FreqlineEdit->setText(QString::number(FrequeLine));


  }

}


void Widget::on_hztSlidProgress_valueChanged(int value) //滑动块帧值变化
{

        ui->lineFrameEdit->setText(QString::number(value));


        ui->CurrentFrame->setText(QString::number(value));
        CurrentFrame=value;


}

void Widget::on_pBtnFrameOk_clicked() //确认当前帧
{
    int CurrentFrame1,FreqlineEdit1;
    CurrentFrame1= ui->lineFrameEdit->text().toInt();
    FreqlineEdit1=ui->FreqlineEdit->text().toInt();

      if((CurrentFrame1<0)|(CurrentFrame1>=nFrameCount)|(FreqlineEdit1<=0)|(FreqlineEdit1>1000))
      {
          QMessageBox::information(this,tr("frame tip information"),"无效！请重新输入播放帧或频率!",QMessageBox::Ok);
          ui->lineFrameEdit->setText(QString::number(CurrentFrame));
          ui->FreqlineEdit->setText(QString::number(FrequeLine));
          ui->CurrentFrame->setText(QString::number(CurrentFrame));


      }
      else
      {
           CurrentFrame=CurrentFrame1;
           FrequeLine=FreqlineEdit1;
          nRet = GetFrame(pFile, pFrame, 10*1024*1024,CurrentFrame); //获取视频文件当前帧
          if(nRet!=0)
          {
              qDebug()<<"获取第"<<CurrentFrame<<"帧失败！";
              CurrentFrame++;
          }


         else
          {
              qDebug()<<"获取第"<<CurrentFrame<<"成功！";
              paintEvent();
              ui->lineFrameEdit->setText(QString::number(CurrentFrame));
              ui->CurrentFrame->setText(QString::number(CurrentFrame));
              ui->hztSlidProgress->setValue(CurrentFrame);
              initime=1000/(FrequeLine);
              mSectimer=initime;
              mlasttimer=initime;
              mbacktimer=initime;
          }
      }



}








//void Widget::on_listWidget_itemPressed(QListWidgetItem *item)
//{
//    qDebug()<<item->text()<<"sadfasdf";
//    ui->listWidget->setToolTip(item->text());
//}
